//
//  ShareOrderPopView.m
//  JiaoX
//
//  Created by jiaoxiu－005 on 15/4/13.
//  Copyright (c) 2015年 北京娇羞科技有限公司. All rights reserved.
//

#import "ShareOrderPopView.h"
#import "ShareOrderPopHeaderView.h"
#import "ShareOrderProdCell.h"
#import "CartProdSub.h"
#import "CustomShowView.h"
@interface ShareOrderPopView()<UITableViewDelegate,UITableViewDataSource>
@property(nonatomic,strong)ShareOrderPopHeaderView *shareOrderPopHeaderView;
@property(nonatomic,strong)UITableView *shareOrderTableView;
@property(nonatomic,strong)UIButton *shareOrderBtn;
@property(nonatomic,strong)ShareProductRes *shareOrderData;
@property(nonatomic,strong)NSArray *productArray;
@property(nonatomic,strong)NSIndexPath *currentSelect;
@end

@implementation ShareOrderPopView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self setBackgroundColor:[UIColor whiteColor]];
        //1.headView
        _shareOrderPopHeaderView = [[UINib nibWithNibName:@"ShareOrderPopHeaderView" bundle:[NSBundle mainBundle]] instantiateWithOwner:self options:nil][0];;
        _shareOrderPopHeaderView.x = 0;
        _shareOrderPopHeaderView.y = 0;
        [self addSubview:_shareOrderPopHeaderView];
        
        //2.tableView
        _shareOrderTableView = [[UITableView alloc]init];
        _shareOrderTableView.delegate = self;
        _shareOrderTableView.dataSource = self;
        [_shareOrderTableView setFrame:CGRectMake(0, CGRectGetMaxY(_shareOrderPopHeaderView.frame), _shareOrderPopHeaderView.width, 130)];
        [self addSubview:_shareOrderTableView];
        
        //3.底部button
        _shareOrderBtn =[[UIButton alloc]init];
        [_shareOrderBtn.titleLabel setFont:[UIFont systemFontOfSize:[DimensUtils getScaleX:15]]];
        [_shareOrderBtn setTitle:@"确定" forState:UIControlStateNormal];
        [_shareOrderBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [_shareOrderBtn setContentMode:UIViewContentModeCenter];
        [_shareOrderBtn setFrame:CGRectMake(JXPublicInset, CGRectGetMaxY(_shareOrderTableView.frame)+20, _shareOrderPopHeaderView.width-JXPublicInset*2, 35)];
        [_shareOrderBtn setBackgroundImage:[UIImage resizedImage:@"btn_login_bg"] forState:UIControlStateNormal];
        [_shareOrderBtn addTarget:self action:@selector(enterShare) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:_shareOrderBtn];
        [self setViewCornerRadius:self];
        [self setFrame:CGRectMake(0, 0,0, CGRectGetMaxY(_shareOrderBtn.frame)+JXPublicInset*2)];
    }
    return self;
}

/**
 * 给view绘制圆角
 */
-(void)setViewCornerRadius :(UIView *)view{
    //设置自己的圆角
    view.layer.cornerRadius = 8;
    view.layer.masksToBounds = YES;
    //给图层添加一个有色边框
    view.layer.borderWidth = 0;
    view.layer.borderColor = [[UIColor colorWithRed:0.52 green:0.09 blue:0.07 alpha:1] CGColor];
}

-(void)setWidth:(CGFloat)width{
    [super setWidth:width];
    [self.shareOrderPopHeaderView setWidth:width];
    [self.shareOrderTableView setWidth:width];
    [self.shareOrderBtn setWidth:width - JXPublicInset*2];
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.productArray.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    ShareOrderProdCell *cell = [ShareOrderProdCell cellWithTableView:tableView];
    [cell setShareProduct:self.productArray[indexPath.row]];
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 64;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    //在赋值之前先记录上一次
    NSIndexPath *temp = self.currentSelect;
    //设置选中,并记录当前
    CartProdSub *product =  self.productArray[indexPath.row];
    if(product.isSelected){
        [product setIsSelected:NO];
    }else{
        if(self.currentSelect){
             [self.productArray[self.currentSelect.row] setIsSelected:NO];
        }
        [product setIsSelected:YES];
        self.currentSelect = indexPath;
    }
    //设置上一个是未选中,确保只能选择一件
    [tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationNone];
    if(temp){
        [tableView reloadRowsAtIndexPaths:@[temp] withRowAnimation:UITableViewRowAnimationNone];
    }
    //处理cell点击效果
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

-(void)setShareProductData:(ShareProductRes *)shareProduct{//设置数据
    self.shareOrderData = shareProduct;
    [self.shareOrderPopHeaderView setSentence:self.shareOrderData.sentence];
    self.productArray =  [self.shareOrderData productList];
    NSUInteger count  = [self.productArray count];
    if(count){
        [self calcUpdateViewHeight:(count>=3)?3:count];
    }else{
        //1.隐藏提示头
        [self.shareOrderPopHeaderView hideTabViewHeadTips];
        //没有数据的时候重新设置上面的位置
        [self.shareOrderPopHeaderView setHeight:self.shareOrderPopHeaderView.height-JXPublicInset*4];
        //2.隐藏tableView
        [self.shareOrderTableView setHidden:YES];
        //3.重新计算下方button的y轴
        [self.shareOrderBtn setY:CGRectGetMaxY(self.shareOrderPopHeaderView.frame)];
        //4.重新计算整个View的高
        [self setHeight:CGRectGetMaxY(self.shareOrderBtn.frame) + JXPublicInset*2];
    }
    [self.shareOrderTableView reloadData];
}

//根据商品数量动态计算View的高度
-(void)calcUpdateViewHeight:(NSUInteger)count{
    [self.shareOrderTableView setHeight:64*count];
    [self.shareOrderBtn setY:CGRectGetMaxY(self.shareOrderTableView.frame)+JXPublicInset];
    [self setHeight:CGRectGetMaxY(self.shareOrderBtn.frame)+JXPublicInset*2];
}

//确定晒单
-(void)enterShare{
    CartProdSub *sub = nil;
    if (self.currentSelect) {
        sub = self.productArray[self.currentSelect.row];
    }
    
    if([self.delegate respondsToSelector:@selector(shareOrder:)]){
        [self.delegate shareOrder:sub];
    }
    
    if([self.superview isKindOfClass:[CustomShowView class]]){
        [((CustomShowView*)self.superview) hideWithanim:YES];
    }
}

@end
